/*
 * socket_address.h
 *
 *  Created on: 2008-7-18
 *      Author: fanliangliang
 */

#ifndef SOCKET_ADDRESS_H_
#define SOCKET_ADDRESS_H_

#include "ip_address.h"
#include <string>

BEGIN_NAMESPACE_TOXIC

using std::string;

class SocketAddress {
public:
	SocketAddress(const IpAddress& address = IpAddress::ANY, uint16_t port = 0);
	SocketAddress(const sockaddr_in& address);
	~SocketAddress();

	SocketAddress(const SocketAddress& other);
	SocketAddress& operator=(const SocketAddress& other);

	void setAddress(const IpAddress& address);
	void setPort(uint16_t port);

	const IpAddress& address() const;
	uint16_t port() const;

	string toString() const;
	bool isValid() const;

	operator const sockaddr*() const;

public:
	static size_t size();

private:
	sockaddr_in* internalAddress() const;

private:
	IpAddress _address;
	uint16_t _port;
	mutable sockaddr_in* _internalAddress;
};


bool operator==(const SocketAddress& lhs, const SocketAddress& rhs);
bool operator!=(const SocketAddress& lhs, const SocketAddress& rhs);
ostream& operator<<(ostream& out, const SocketAddress& address);

END_NAMESPACE_TOXIC

#endif /* SOCKET_ADDRESS_H_ */
